function  [ hmmsParam ] = trainData(handles,folder_path,models)
%function  [ hmmsParam ] = trainData(handles,folder_path,models)
  
    %Get valid images
    validImages = getValidImages(folder_path);
    %Get the number of images
    nimages = size(validImages,1);
    %Get the number of HMM Models to trained
    nmodels = size(models, 2);
    %Allocate the storage for training result, each HMM model separately
    training_results = cell(nmodels,1);
    %{
    for i = 1 : nmodels
        training_results{i} = zeros(nimages, models{i}.obsMatrixRows, models{i}.obsMatrixCols);
    end
    %}
    
    %Set the plotting structure, currently it is 3 image per rows
    plotRows = ceil(nimages/3);
    
    %Iterate through images
    for i = 1 : nimages
        %Read the image
        img = imread(strcat(folder_path,'\', validImages(i,:)));
        %Threshold to a binary image
        img = threshold2Bin(img);
        %Get the letter boundary in the image
        [rmin rmax cmin cmax] = bboxletter(img,1);
        %Image cropped to letter area
        letterImg = img(rmin:rmax, cmin:cmax);
        %Plot the image
        subplot(plotRows,3, i,'Parent',handles.pnlAllTrainingImagesInFolder);
        imshow(letterImg);
        observations = letterObservations(letterImg, models, handles);
        for j = 1 : nmodels
            training_results {j}(i,:,:) = observations{j,:}
        end
        
        isContinuous =  get(handles.rbContinuous,'Value');
        
        if ~isContinuous
            %Wait for user to press the button
            waitfor(handles.btnTrain, 'UserData', 1);
            %Set the value back to 0 -corresponds to not pressed yet-
            set(handles.btnTrain, 'UserData', 0);
        end
        
    end
    
    %{
    hmmsParam = cell(nmodels,5);
    %Train HMM according to training results we have achieved
    for i = 1 : nmodels
        %Get models parameters M and N
        N = models{i}.N;
        M = models{i}.M;
        [ prior, transmat, obsmat ] = trainHMM (training_results{i}, M, N);
        hmmsParam(i,:) = {M, N, prior, transmat, obsmat};
    end
    hmmsParam{1,:}
    %}
end

